#include "StdAfx.h"
#include "TaskEntryListTreeItem.h"
#include "GTDTreeCtrl.h"
#include "../core/guid.h"
#include "../core/GtdDOMItem.h"
#include "mxgtd.h"
#include "DlgTaskPropEdit.h"


enum ItemDataType{
	_UNPROJ_TASKROOT,
	_PROJ_TASKROOT,
	_TASK,
};

class TaskListTreeItemData : public TreeItemImpl
{
public:	
	TaskListTreeItemData(ItemDataType t, const CGuid& id){   mt = t, gid = id; }
	TaskListTreeItemData(ItemDataType t){ mt = t; }
	ItemDataType mt;
	CGuid gid;
	virtual void OnDblClick(const CTreeCtrlItem& ctrlItem)
	{
		if(mt == _TASK)
		{
			if(GtdDOMItem* pItem = theApp.getGTDDOM().getItem(gid))
			{
				if(GtdTaskEntry* pEntry = pItem->toTaskEntry())
				{
					CDlgTaskPropEdit editProp(pEntry, ctrlItem.m_ctrl);
					if(IDOK == editProp.DoModal())
					{		
						
						TaskEntryListTreeItem root = (ctrlItem.GetRoot());
						root.ResetTaskEntryItem(pEntry);
					}
				}
			}		

		}
	}

};

TaskEntryListTreeItem TaskEntryListTreeItem::findItem( const CGuid&guid ) const
{
	if(guid.isNull())
		return CTreeCtrlItem();

	CTreeCtrlItem end = GetPreOrderEnd();
	CTreeCtrlItem next  = *this;
	while(next!=end)
	{
		if(TaskListTreeItemData* pImpl = (TaskListTreeItemData*)next.GetImpl() )
		{
			if(pImpl->gid == guid)
				return next;
		}
		next = next.GetPreOrderNext();
	}
	return CTreeCtrlItem();
}

TaskEntryListTreeItem TaskEntryListTreeItem::findFirstItem( ItemDataType t ) const
{
	CTreeCtrlItem end = GetPreOrderEnd();
	CTreeCtrlItem next  = *this;
	while(next!=end)
	{
		if(TaskListTreeItemData* pImpl = (TaskListTreeItemData*)next.GetImpl() )
		{
			if(pImpl->mt == t)
				return next;
		}
		next = next.GetPreOrderNext();
	}
	return CTreeCtrlItem();
}

void TaskEntryListTreeItem::AddTaskEntryItem( GtdTaskEntry *pEntry )
{
	ASSERT(!findItem(pEntry->m_id));
	TaskEntryListTreeItem prjItem = findItem(pEntry->mPrj);
	if(!prjItem)
	{
		prjItem = findFirstItem(_UNPROJ_TASKROOT);
	}
	ASSERT(prjItem);
	TaskEntryListTreeItem taskItem = prjItem.AddChild(pEntry->msName);
	taskItem.SetImpl( new TaskListTreeItemData(_TASK,pEntry->m_id) );
	prjItem.Expand();
}

void TaskEntryListTreeItem::LoadTree( CGTDTreeCtrl& ctrl )
{
	ctrl.DeleteAllItems();
	GtdDOM& dom = theApp.getGTDDOM();
	//load unprojected task
	TaskEntryListTreeItem root = CTreeCtrlItem::GetRoot(ctrl);
	TaskEntryListTreeItem unPrjItem = root.AddChild(_T("UnProjected:"));
	unPrjItem.SetExpandImage(1);
	unPrjItem.SetImage(0,0);
	//unPrjItem.SetState(1);
	unPrjItem.SetImpl(new TaskListTreeItemData(_UNPROJ_TASKROOT));
	//unPrjItem.AddChild(_T("Test"));
	unPrjItem.Expand();
	//unPrjItem.SetData();

	root = CTreeCtrlItem::GetRoot(ctrl);
	GuidList entrylist= dom.GetTaskEntryList();
	for(GuidList::const_iterator itr = entrylist.begin();itr!=entrylist.end(); ++itr)
	{
		if(GtdTaskEntry* pEntry = dom.GetTaskEntry(*itr) )
		{
			root.AddTaskEntryItem(pEntry);
		}
		
	}
}

void TaskEntryListTreeItem::ResetTaskEntryItem( GtdTaskEntry* pEntry )
{

	TaskEntryListTreeItem prjItem = findItem(pEntry->mPrj);
	if(!prjItem)
	{
		prjItem = findFirstItem(_UNPROJ_TASKROOT);
	}
	ASSERT(prjItem);
	TaskEntryListTreeItem taskItem = prjItem.findItem(pEntry->m_id);
	ASSERT(taskItem);
	if(taskItem)
	{
		taskItem.SetText(pEntry->msName);
		prjItem.Expand();
	}
	
}
